﻿package com.fzb.forum4j.util;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Vector;

import javax.mail.Address;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class Mail {

	private String displayName; //邮件发送的人的名字
	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	public String getTo() {
		return to;
	}

	public void setTo(String to) {
		this.to = to;
	}

	public String getFrom() {
		return from;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public String getSmtpServer() {
		return smtpServer;
	}

	public void setSmtpServer(String smtpServer) {
		this.smtpServer = smtpServer;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSubject() {
		return subject;
	}

	public void setSubject(String subject) {
		this.subject = subject;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public boolean isIfAuth() {
		return ifAuth;
	}

	public void setIfAuth(boolean ifAuth) {
		this.ifAuth = ifAuth;
	}

	public String getFilename() {
		return filename;
	}

	public void setFilename(String filename) {
		this.filename = filename;
	}

	public Vector getFile() {
		return file;
	}

	public void setFile(Vector file) {
		this.file = file;
	}

	public Mail(Map<String,String> webSite,String to,String mailTemplate,String append,String title){
		this.displayName=webSite.get("mail_displayName");
		this.to=to;
		this.from=webSite.get("mail_from");
		this.smtpServer=webSite.get("mail_smtpServer");
		this.username=webSite.get("mail_userName");
		this.password=webSite.get("mail_password");
		String content= webSite.get("mail_mailTemplate_"+mailTemplate).replace("{append}", append);
		content=content.replace("{userMail}", to);
		this.subject=title;
		this.content=content;
		this.ifAuth=true;
		this.port=25;
	}

	public Mail(String displayName, String to, String from, String smtpServer,
			String username, String password, String subject, String content,
			boolean ifAuth, String filename, Vector file) {
		this.displayName = displayName;
		this.to = to;
		this.from = from;
		this.smtpServer = smtpServer;
		this.username = username;
		this.password = password;
		this.subject = subject;
		this.content = content;
		this.ifAuth = ifAuth;
		this.filename = filename;
		this.file = file;
		this.port=465;
	}
	public Mail(){};
	
	private String to;//邮件的接受者
	private String from;//发送者
	private String smtpServer;//SMTP 的服务器地址
	private String username;//用户名
	private String password;//密码
	private String subject;//
	private String content;//文本
	private boolean ifAuth; // 服务器是否要身份认证
	private String filename = "";
	private Integer port;
	private Vector file = new Vector(); // 用于保存发送附件的文件名的集合

	public static void main(String[] args) {
		Mail mail= new Mail("小春", "342844335@qq.com", "bbs_server@94fzb.com", "smtp.qq.com", "wozcc94", "bbs_server", "标题", "内容", true, "小春", new Vector());
		mail.sendMail();
	}

	public  Map<String, String> sendMail() {
		Map<String, String> map=new HashMap<String, String>();
		map.put("status", "ok");
		Session session=null;
		//在系统中生成一个配置文件用于检测邮箱的可用性
		Properties prop=System.getProperties();
		prop.put("mail.smtp.host", smtpServer);   
		if(ifAuth){ 
			//服务器需要身份认证   
            prop.put("mail.smtp.auth","true");
            prop.put("mail.smtp.port",port);
            SmtpAuth smtpAuth=new SmtpAuth(username,password);   
            session=Session.getDefaultInstance(prop, smtpAuth);    
        }else{   
            prop.put("mail.smtp.auth","false");   
            session=Session.getDefaultInstance(prop, null);   
        }   
		session.setDebug(true);
		Transport tran=null;
		Message message=new MimeMessage(session);
		try {
			Address address=new InternetAddress(from, displayName);
			message.setFrom(address);
			InternetAddress[] addressto={new InternetAddress(to)};
			message.setRecipients(Message.RecipientType.TO, addressto);
			message.setSubject(subject);
			Multipart mp = new MimeMultipart(); 
			MimeBodyPart mbp = new MimeBodyPart();   
			mbp.setContent(content, "text/html;charset=utf-8");   
			mp.addBodyPart(mbp);
			message.setContent(mp); 
	        message.setSentDate(new Date());  
	        message.saveChanges();    
            tran= session.getTransport("smtp");   
            tran.connect(smtpServer, username,password);   
            tran.sendMessage(message, message.getAllRecipients());   
            tran.close();

		} catch (AddressException e) {
			map.put("status", "failed");
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			map.put("status", "failed");
			e.printStackTrace();
		} catch (MessagingException e) {
			map.put("status", "failed");
			e.printStackTrace();
		}  
		return map;


	}
}
